﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BudgetMealsModels.Entities;
using BudgetMealsModels.Abstract;
using System.Data.Entity;
using BudgetMealsModels.Concrete;
using BudgetMealsUI.Models;

namespace BudgetMealsUI.Controllers
{
    public class MealController : Controller
    {
        private IMealRepository _mealRepository;
        private IRecipeRepository _recipeRepository;


        public MealController(IMealRepository mealRepository, IRecipeRepository recipeRepository)
        {
            _mealRepository = mealRepository;
            _recipeRepository = recipeRepository;
        }
        public ActionResult ShowMeal(int ID)
        {
            Meal meal = _mealRepository.Meals.FirstOrDefault(x => x.MealID == ID);
            return View(meal);

        }
        public ActionResult AllMeals(int page = 1)
        {
            List<Meal> meals = _mealRepository.Meals.ToList();
            const int pageSize = 20;
            ListViewModel<Meal> model = new ListViewModel<Meal>()
            { 
                PageItems = meals.OrderBy(x => x.Name).Skip(pageSize * (page - 1)).Take(pageSize), 
                Info = new PagingInfo() 
                { CurrentPage = page, ItemsPerPage = pageSize, TotalItems = meals.Count } 
            };

            return View(model);
        }
        public ActionResult Index(int page = 1)
        {
            const int pageSize = 10;
            ListViewModel<Meal> viewModel = new ListViewModel<Meal>
            {
                Info = new PagingInfo { CurrentPage = page, ItemsPerPage = pageSize, TotalItems = _mealRepository.Meals.Count() },
                PageItems = _mealRepository.Meals.OrderBy(x => x.MealID).Skip(pageSize * (page - 1)).Take(pageSize)
            };

            return View(viewModel);
        }
        public ActionResult Delete(int mealId = 0)
        {
            Meal meal = _mealRepository.Meals.FirstOrDefault(m => m.MealID == mealId);

            _mealRepository.DeleteMeal(meal, (int) Session["CurrentUser"]);

            return RedirectToAction("Index");
        }
        [HttpGet]
        public ActionResult Edit(int mealId = 0)
        {
            Meal meal = _mealRepository.Meals.FirstOrDefault(m => m.MealID == mealId);
            FormMeal formMeal = null;

            if (meal != null && meal.MealID != 0)
                formMeal = new FormMeal 
                { 
                    ID = mealId, 
                    Appetizer = meal.Appetizer.Name, 
                    MainCourse = meal.MainCourse.Name, 
                    Dessert = meal.Dessert.Name, 
                    AppetizerID = meal.Appetizer.RecipeID,
                    MainCourseID = meal.MainCourse.RecipeID,
                    DessertID = meal.Dessert.RecipeID,
                    Name = meal.Name
                };
            else
                formMeal = new FormMeal();

            return View(formMeal);
        }
        [HttpPost]
        public ActionResult Edit(FormMeal formMeal)
        {
            if (ModelState.IsValid)
            {
                var appetizer = _recipeRepository.Recipes.FirstOrDefault(r => r.RecipeID == formMeal.AppetizerID);
                var mainCourse = _recipeRepository.Recipes.FirstOrDefault(r => r.RecipeID == formMeal.MainCourseID);
                var dessert = _recipeRepository.Recipes.FirstOrDefault(r => r.RecipeID == formMeal.DessertID);

                if (appetizer == null || mainCourse == null || dessert == null)
                {
                    ModelState.AddModelError(string.Empty, "Appetizer, Main Course, or Dessert was not found.");
                    
                    return View("Edit");
                }
                else
                {
                    var meal = new Meal
                    {
                        MealID = formMeal.ID,
                        Name = formMeal.Name,
                        Appetizer = appetizer,
                        MainCourse = mainCourse,
                        Dessert = dessert
                    };

                    _mealRepository.SaveMeal(meal, (int) Session["CurrentUser"]);

                    return RedirectToAction("Profile", "User", new { Id = (int)Session["CurrentUser"] });
                }
            }
            else
            {
                return View("Edit");
            }
        }
    }
}
